{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intersection of Linked Lists\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/XvnHbAqjHEEOaKO48iB7JQ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given two singly linked lists. The lists intersect at some node. Find, and return the node. Note: the lists are non-cyclical."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```{text}\n",
    "A = 1 -> 2 -> 3 -> 4\n",
    "B = 6 -> 3 -> 4\n",
    "```\n",
    "\n",
    "This should return 3 (you may assume that any nodes with the same value are the same node)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "class Node(object):\n",
    "    def __init__(self, val):\n",
    "        self.val = val\n",
    "        self.next = None\n",
    "        pass\n",
    "    \n",
    "    def prettyPrint(self):\n",
    "        c = self\n",
    "        while c is not None:\n",
    "            print(c.val)\n",
    "            c = c.next\n",
    "            pass\n",
    "        pass\n",
    "    \n",
    "    pass\n",
    "\n",
    "\n",
    "def intersection(a: Node, b: Node) -> Node:\n",
    "    # Fill this in. \n",
    "    pointerA = a\n",
    "    while pointerA is not None:\n",
    "        pointerB = b\n",
    "        while pointerB is not None:\n",
    "            if pointerA.val == pointerB.val:\n",
    "                return pointerA\n",
    "            pointerB = pointerB.next\n",
    "            pass\n",
    "        pointerA = pointerA.next\n",
    "        pass\n",
    "    pass\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    a = Node(1)\n",
    "    a.next = Node(2)\n",
    "    a.next.next = Node(3)\n",
    "    a.next.next.next = Node(4)\n",
    "    \n",
    "    b = Node(6)\n",
    "    b.next = a.next.next\n",
    "    \n",
    "    c = intersection(a, b)\n",
    "    c.prettyPrint()\n",
    "    pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
